package org.liushengpei.com.util;

import io.minio.*;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 操作minio工具类
 */
@Component
public class MinioUtils {

    @Resource
    private MinioClient minioClient;

    /**
     * 上传文件
     *
     * @param bucketName  桶的名称
     * @param fileName    文件名称
     * @param inputStream 文件输入流
     */
    public void uploadFile(String bucketName, String fileName, FileInputStream inputStream) {
        try {
            minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(bucketName)
                            .object(fileName).stream(inputStream, inputStream.available(), -1)
                            .build());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    /**
     * 获取文件url路径
     *
     * @param bucketName 桶的名称
     * @param fileName   文件名称
     */
    public String getFileUrl(String bucketName, String fileName) {
        String fileUrl = "";
        try {
            fileUrl = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
                    .bucket(bucketName)
                    .object(fileName)
                    .method(Method.GET)
                    .expiry(30, TimeUnit.SECONDS)
                    .build());
            if (fileUrl.contains("?")) {
                String[] split = fileUrl.split("\\?");
                fileUrl = split[0];
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileUrl;
    }

    /**
     * 上传文件并返回文件地址
     *
     * @param bucketName  桶的名称
     * @param fileName    文件名称
     * @param inputStream 文件输入流
     */
    public String uploadFileReturnUrl(String bucketName, String fileName, InputStream inputStream) {
        String fileUrl = "";
        long currentTimeMillis = System.currentTimeMillis();
        //设置文件名称
        String newFileName = currentTimeMillis + fileName.substring(fileName.lastIndexOf("."));
        try {
            minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(bucketName)
                            .object(newFileName).stream(inputStream, inputStream.available(), -1)
                            .build());
            //获取文件路径
            fileUrl = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
                    .bucket(bucketName)
                    .object(newFileName)
                    .method(Method.GET)
                    .expiry(30, TimeUnit.SECONDS)
                    .build());
            if (fileUrl.contains("?")) {
                String[] split = fileUrl.split("\\?");
                fileUrl = split[0];
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileUrl;
    }

    /**
     * 判断存储桶是否存在
     *
     * @param bucketName 桶的名称
     */
    public boolean bucketExists(String bucketName) {
        boolean off = false;
        BucketExistsArgs btest2 = BucketExistsArgs.builder().bucket(bucketName).build();
        try {
            off = minioClient.bucketExists(btest2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return off;
    }

    /**
     * 创建桶
     *
     * @param bucketName 桶名称
     */
    public void createBucket(String bucketName) {
        try {
            //判断桶是否存在
            boolean bucketExists = this.bucketExists(bucketName);
            if (!bucketExists) {
                MakeBucketArgs makeBucketArgs = MakeBucketArgs.builder().bucket(bucketName).build();
                minioClient.makeBucket(makeBucketArgs);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询所有桶信息
     */
    public List<Bucket> bucketListInfo() {
        List<Bucket> bucketList = null;
        try {
            bucketList = minioClient.listBuckets();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bucketList;
    }

    /**
     * 删除存储桶
     *
     * @param bucketName 存储桶名称
     */
    public void removeBucket(String bucketName) {
        try {
            RemoveBucketArgs removeBucketArgs = RemoveBucketArgs.builder().bucket(bucketName).build();
            minioClient.removeBucket(removeBucketArgs);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
